import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Solution1 {
    public int reversePairs(int[] nums) {
        return mergeSort(nums, 0, nums.length - 1);
    }

    public int mergeSort(int[] nums, int left, int right) {
        if (left >= right) return 0;
        int ret = 0, mid = (left + right) / 2;
        //[left,mid] [mid+1,right]
        ret += mergeSort(nums, left, mid);
        ret += mergeSort(nums, mid + 1, right);

        int[] tmp = new int[right - left + 1];
        int cur1 = left, cur2 = mid + 1, i = 0;

        while (cur2 <= right) {
            while (cur1 <= mid && nums[cur2] >= nums[cur1] / 2.0) {
                cur1++;
            }
            if (cur1 > mid) {
                break;
            }
            ret += mid - cur1 + 1;
            cur2++;
        }

        cur1 = left;
        cur2 = mid + 1;
        while (cur1 <= mid && cur2 <= right) {
            if (nums[cur1] > nums[cur2]) {
                tmp[i++] = nums[cur2++];
            } else {
                tmp[i++] = nums[cur1++];
            }
        }

        while (cur1 <= mid) {
            tmp[i++] = nums[cur1++];
        }
        while (cur2 <= right) {
            tmp[i++] = nums[cur2++];
        }
        for (int j = left; j <= right; j++) {
            nums[j] = tmp[j - left];
        }
        return ret;
    }


}


class Solution2 {
    int[] tmp;

    public int reversePairs(int[] nums) {
        int n = nums.length;
        tmp = new int[n];
        return mergeSort(nums, 0, nums.length - 1);
    }

    public int mergeSort(int[] nums, int left, int right) {
        if (left >= right) return 0;
        int ret = 0, mid = (left + right) / 2;
        //[left,mid] [mid+1,right]
        ret += mergeSort(nums, left, mid);
        ret += mergeSort(nums, mid + 1, right);

        int cur1 = left, cur2 = mid + 1, i = 0;

        while (cur1 <= mid && cur2 <= right) {
            if (nums[cur2] < nums[cur1] / 2.0) {
                ret += mid - cur1 + 1;
                cur2++;
            } else {
                cur1++;
            }
        }

        cur1 = left;
        cur2 = mid + 1;
        while (cur1 <= mid && cur2 <= right) {
            if (nums[cur1] > nums[cur2]) {
                tmp[i++] = nums[cur2++];
            } else {
                tmp[i++] = nums[cur1++];
            }
        }

        while (cur1 <= mid) {
            tmp[i++] = nums[cur1++];
        }
        while (cur2 <= right) {
            tmp[i++] = nums[cur2++];
        }
        for (int j = left; j <= right; j++) {
            nums[j] = tmp[j - left];
        }
        return ret;
    }
}

class Solution {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode cur1 = l1,cur2 = l2;
            ListNode newHead = new ListNode();
            ListNode cur3 = newHead;
            int t = 0;
            while(cur1!=null || cur2!=null||t!=0) {
                int a = cur1==null?0:cur1.val;
                int b = cur2==null?0:cur2.val;
                int k = a+b+t;
                ListNode node = new ListNode(k%10);
                t = k/10;
                cur3.next = node;
                if(cur1!=null) {
                    cur1 = cur1.next;
                }
                if(cur2!=null) {
                    cur2 = cur2.next;
                }
                cur3 = cur3.next;
            }
            return newHead.next;
        }
    }

    public ListNode swapPairs1(ListNode head) {
        ListNode newhead = new ListNode();
        newhead.next = head;
        head = newhead;
        ListNode prev = head.next;
        if(prev == null) {
            return newhead.next;
        }
        ListNode next = prev.next;
        while(head!=null && prev!=null && next!=null) {
            prev.next = next.next;
            head.next = next;
            next.next = prev;
            head = prev;
            prev = head.next;
            if(prev!=null) {
                next = prev.next;
            }
        }
        return newhead.next;
    }

    //递归版本
    private void swap(ListNode head,ListNode prev,ListNode next) {
        if(head == null || prev == null || next == null) return;
        prev.next = next.next;
        head.next = next;
        next.next = prev;
        head = prev;
        prev = head.next;
        if(prev!=null) {
            next = prev.next;
        }
        swap(head,prev,next);
    }
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode newhead = new ListNode();
        newhead.next = head;
        swap(newhead,newhead.next,newhead.next.next);
        return newhead.next;
    }
}

public class Test {
    public static void main(String[] args) {

    }

    public static void main1(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 100; i++) {
            service.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello");
                }
            });
        }
    }
}
